﻿@page "/Grid/DataBinding/LargeQueryable"
@layout DataProviderAccessArea<IRentInfoDataProvider>
@using BlazorDemo.Pages.Grid.Filtering
@using static BlazorDemo.Pages.Grid.Filtering.Grid_Filtering_ColumnFilterMenu_CustomRange;

<DemoPageSectionComponent Id="Grid-DataBinding-LargeQueryable" ShowSizeMode="true">
    <ChildContentWithParameters Context="Params">
        @inherits OwningComponentBase
        @inject Microsoft.Extensions.Configuration.IConfiguration Configuration
        
        <DataProviderAccessAreaContainer DataProvider="@RentInfoDataProvider">
        <div class="grid-container">
            <DxGrid @ref="Grid"
                    Data="Data"
                    ShowGroupPanel="true"
                    VirtualScrollingEnabled="VirtualScrollingEnabled" PageSize="20"
                    FilterMenuButtonDisplayMode="GridFilterMenuButtonDisplayMode.Always"
                    SizeMode="Params.SizeMode"
                    ColumnResizeMode="GridColumnResizeMode.NextColumn" TextWrapEnabled="false">
                <Columns>
                    <DxGridDataColumn FieldName="State" MinWidth="80" Width="10%" />
                    <DxGridDataColumn FieldName="Area" Width="12%" />
                    <DxGridDataColumn FieldName="City" Caption="County" MinWidth="100" Width="17%" />
                    <DxGridDataColumn FieldName="Name" Caption="Location" MinWidth="100" Width="17%" />
                    <DxGridDataColumn FieldName="Year" DisplayFormat="0" MinWidth="80" Width="10%" />
                    <DxGridDataColumn FieldName="Population" DisplayFormat="#,0" MinWidth="100">
                        <FilterMenuTemplate>
                            <Grid_Filtering_ColumnFilterMenu_CustomRange FilterContext="context" Items="PopulationIntervals" />
                        </FilterMenuTemplate>
                    </DxGridDataColumn>
                    <DxGridDataColumn FieldName="Bedrooms" Width="12%">
                        <EditSettings>
                            <DxComboBoxSettings Data="BedroomDisplayValues" />
                        </EditSettings>
                    </DxGridDataColumn>
                    <DxGridDataColumn FieldName="Rent" MinWidth="80" DisplayFormat="C0">
                        <FilterMenuTemplate>
                            <Grid_Filtering_ColumnFilterMenu_CustomRange FilterContext="context" Items="RentIntervals" />
                        </FilterMenuTemplate>
                    </DxGridDataColumn>
                </Columns>
                <GroupSummary>
                    <DxGridSummaryItem FieldName="Rent" SummaryType="GridSummaryItemType.Avg"/>
                </GroupSummary>
                <TotalSummary>
                    <DxGridSummaryItem FieldName="Rent" SummaryType="GridSummaryItemType.Avg"/>
                </TotalSummary>
                <ToolbarTemplate>
                    <DxToolbar ItemRenderStyleMode="ToolbarRenderStyleMode.Plain" SizeMode="Params.SizeMode">
                        <Items>
                            <DxToolbarItem BeginGroup="true" Alignment="ToolbarItemAlignment.Left">
                                <Template Context="toolbar_item_context">
                                    <div class="d-flex flex-row align-items-center h-100">
                                        <DxCheckBox @bind-Checked="VirtualScrollingEnabled">Enable Virtual Scrolling</DxCheckBox>
                                    </div>
                                </Template>
                            </DxToolbarItem>
                        </Items>
                    </DxToolbar>
                </ToolbarTemplate>
            </DxGrid>
        </div>
        </DataProviderAccessAreaContainer>
    </ChildContentWithParameters>
    @code {
        static IReadOnlyList<CustomRangeFilterItem> PopulationIntervals { get; } = CustomRangeFilterItem.CreateIntervals("Population", 10, 6, false, "{0:N0}");
        static IReadOnlyList<CustomRangeFilterItem> RentIntervals { get; } = CustomRangeFilterItem.CreateIntervals("Rent", 200, 10, true, "{0:c}");
        static IReadOnlyList<string> BedroomDisplayValues { get; } = new[] { "Studio", "One Bedroom", "Two Bedrooms", "Three Bedrooms", "Four Bedrooms" };
        IRentInfoDataProvider RentInfoDataProvider { get; set; }
        RentInfoDataService RentInfoDataService { get; set; }

        bool VirtualScrollingEnabled { get; set; } = true;
        IGrid Grid { get; set; }
        object Data { get; set; }

        protected override void OnInitialized() {
            // Refer to https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.components.owningcomponentbase
            RentInfoDataProvider = ScopedServices.GetRequiredService<IRentInfoDataProvider>();
            RentInfoDataService = ScopedServices.GetRequiredService<RentInfoDataService>();

            var connectionString = ConnectionStringUtils.GetGridLargeDataConnectionString(Configuration);
            if(string.IsNullOrEmpty(connectionString)) return;

            var dataSource = new GridDevExtremeDataSource<AreaRentInfo>(RentInfoDataService.GetAreaRentInfo());
            dataSource.CustomizeLoadOptions = (loadOptions) => {
                // If underlying data is a large SQL table, specify PrimaryKey and PaginateViaPrimaryKey.
                // This can make SQL execution plans more efficient.
                loadOptions.PrimaryKey = new[] { "Oid" };
                loadOptions.PaginateViaPrimaryKey = true;
            };

            Data = dataSource;
        }

        protected override async Task OnAfterRenderAsync(bool firstRender) {
            if(firstRender) {
                await Grid.WaitForDataLoadAsync();
                Grid.ExpandGroupRow(1);

                await Grid.WaitForDataLoadAsync();
                Grid.ExpandGroupRow(2);
            }
        }
    }
</DemoPageSectionComponent>
